package com.situ.gym.controller;

import com.situ.gym.util.AliOSSUtil;
import com.situ.gym.util.Result;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
public class UploadController {

    //MultipartFile file封装了所有和图片上传信息
    @RequestMapping("/upload")
    public Result upload(MultipartFile file) {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String filename = file.getOriginalFilename();
        String extension = filename.substring(filename.lastIndexOf("."));
        String newFileName = uuid + extension;
        String url = "";
        try {
            url = AliOSSUtil.uploadFile(newFileName,file.getInputStream());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println(newFileName);
        return Result.ok("",url);
    }

    public static void main(String[] args) {
        String uuid = UUID.randomUUID().toString();
        //9786c548-6dc7-4557-8521-816c109b1554
        System.out.println(uuid);
        //9786c5486dc745578521816c109b1554
        System.out.println(uuid.replaceAll("-", ""));
    }
}
